<template>
  <div class="search-input">
    <i class="icon-search"></i>
    <input type="text" class="input-inner" v-model="query" :placeholder="placeholder" />
    <i class="icon-dismiss" v-show="query" @click="clear"></i>
  </div>
</template>
<script>
import { debounce } from '@/js/debounce'
export default {
  name: 'SearchInput',
  props: {
    modelValue: String,
    placeholder: {
      type: String,
      default: '请输入歌手名或歌曲名'
    }
  },
  data () {
    return {
      query: this.modelValue
    }
  },
  created () {
    const tempFunc = debounce(300, this.updateModelValue)
    this.$watch('query', (newQuery) => {
      tempFunc(newQuery)
    })
  },
  methods: {
    updateModelValue (newVal) {
      this.$emit('update:modelValue', newVal)
    },
    clear () {
      this.query = ''
    }
  },
  watch: {
    modelValue (newVal) {
      this.query = newVal
    }
  }
}
</script>
<style lang="scss" scoped>
.search-input {
  display: flex;
  align-items: center;
  box-sizing: border-box;
  width: 100%;
  padding: 0 .255864rem;
  height: 1.364606rem;
  background: $color-highlight-background;
  border-radius: .255864rem;
  .icon-search {
    font-size: 1.023454rem;
    color: $color-text-d;
  }
  .input-inner {
    flex: 1;
    margin: 0 .21322rem;
    line-height: .767591rem;
    background: $color-highlight-background;
    color: $color-text;
    font-size: $font-size-medium;
    outline: 0;
    &::placeholder {
      color: $color-text-d;
    }
  }
  .icon-dismiss {
    font-size: .682303rem;
    color: $color-text-d;
  }
}
</style>
